#!/usr/bin/bash
##############################################################
# License Terms
# 
# "This software" is SimpleSMF, or sometimes spelled simplesmf, presently available from http://code.google.com/p/simplesmf
# 
# Copyright (c) 2012 CleverTrove, LLC
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
##############################################################
#
PATH=/usr/gnu/bin:/usr/bin:/usr/sbin:/sbin
#
start() {
  #
  # USER MUST EDIT AND CUSTOMIZE THIS SECTION
  # The commands below might serve as a useful example reference.
  #
  # In this example, myvboxguest1 and myvboxguest2 will both startup simultaneously.
  VBoxHeadless -s myvboxguest1 &
  VBoxHeadless -s myvboxguest2 &
  #
  # Give myvboxguest1 and myvboxguest2 a 2 minute headstart before the next stuff
  sleep 120
  #
  # Perhaps for myvboxguest3, you are using a zvol for storage, via "VBoxManage internalcommands createrawvmdk"
  # You might need to chown the zvol at every boot (or zfs/zpool mount, unmount, import export)
  # You might want to do something like this before starting the guestvm:
  #    Notice: Remember, this script is being run as vboxuser.
  #            So vboxuser would need NOPASSWD permission for "chown" in sudoers, for the "chown" to run
  #            It would be recommended, that you create a wrapper script for that purpose,
  #            and grant passwordless sudo access to the script instead of directly to chown
  #            so vboxuser can't just "chown" anything they want.
  #            Example sudoers config line:
  #               vboxuser ALL= NOPASSWD: /lib/svc/method/simplesmf-virtualbox-guest-control-chown-wrapper.sh
  #            (Obviously, you would need to create that wrapper script, a job left to your own exercise.)
  #    But in my brainless example, I'm just assuming "sudo chown" will work somehow:
  sudo chown vboxuser /dev/zvol/rdsk/vmstorage/myvboxguest3_zvol
  VBoxHeadless -s myvboxguest3 &
  #
  # Perhaps you want to automatically snapshot myvboxguest4 before starting it...
  # Remember, this script is being run as vboxuser. You should probably grant vboxuser
  # permission to snapshot this volume.
  zfs snapshot vmstorage/myvboxguest4@`date '+%F-%Hh%M'`
  #
  # If you like, run some custom service monitor program, such as httping, or echoping, or whatever...
  # to delay myvboxguest4 startup until after some custom service is being provided...
  # Maybe myvboxguest4 depends heavily on NIS or LDAP or something... here you can insert any command
  # you wish, to delay the start of myvboxguest4 until those services are available.
  # Beware the SMF startup timeout.  (Maybe go customize it in simplesmf-virtualbox-guest-control.xml )
  VBoxHeadless -s myvboxguest4 &
}
stop() {
  #
  # USER MUST EDIT AND CUSTOMIZE THIS SECTION
  # The commands below might serve as a useful example reference.
  #
  # See VBoxManage --help or the online manual http://www.virtualbox.org/manual/ch08.html#vboxmanage-controlvm
  # savestate is like "hibernate"
  VBoxManage controlvm myvboxguest4 savestate
  #
  # acpipowerbutton will signal the guestvm to shutdown - depending on how the guestvm is configured to handle the power button.
  VBoxManage controlvm myvboxguest3 acpipowerbutton
  #
  # perhaps you want to use some other shutdown mechanism...
  ssh -i ~/.ssh/guest2rsakey root@myvboxguest2 'shutdown -h now'
  ssh -i ~/.ssh/guest1rsakey admin@myvboxguest1 'sudo init 0'
  #
  # It's probably a good idea to wait till all runningvms have completely terminated before we exit
  # (By exiting with status 0, we've indicated the simplesmf-virtualbox-guest-control service has successfully stopped.)
  while [ `VBoxManage list runningvms | wc -l` -gt 0 ] ; do
    sleep 5
    # If we loop too many times, it might be nice to try signaling the admin somehow
    # Otherwise, the SMF timeout will eventually kick in
  done
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  refresh)
    # If somebody refreshes the service, I guess they just edited this script, and they want to make some new VM start.
    # If you signal a VM to start that has already started, it just throws a harmless error message the second time.
    start
    ;;
  *)
    echo "Usage: $0 start|stop|restart|refresh"
esac
